package com.dynabook.ems.model.enums;

import org.apache.commons.lang3.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public enum RedisKeyEnum {


    WORKER_ORDER_DEATIL_PREFIX("WORKER_ORDER_DEATIL_PREFIX", "WORKER_ORDER:DEATIL:"),
    WORKER_ORDER_BOM_PREFIX("WORKER_ORDER_BOM_PREFIX", "WORKER_ORDER:BOM:"),
    WORKER_ORDER_APPROVAL_PREFIX("WORKER_ORDER_APPROVAL_PREFIX", "WORKER_ORDER:APPROVAL:"),
    WORKER_ORDER_NEW_PREFIX("WORKER_ORDER_NEW_PREFIX", "WORKER_ORDER:NEW:"),

    // 工单状态同步相关Redis Key
    DETAIL_APPROVAL_STATUS("DETAIL_APPROVAL_STATUS", "WORKER_ORDER:DETAIL_APPROVAL:"),
    BOM_STATUS_TRACKING("BOM_STATUS_TRACKING", "WORKER_ORDER:BOM_STATUS:"),
    REQUEST_DETAIL_MAPPING("REQUEST_DETAIL_MAPPING", "WORKER_ORDER:REQUEST_MAPPING:"),
    DETAIL_WORKORDER_MAPPING("DETAIL_WORKORDER_MAPPING", "WORKER_ORDER:DETAIL_MAPPING:"),
    WORK_ORDER_LOCK("WORK_ORDER_LOCK", "WORKER_ORDER:LOCK:");

    private final String text;

    private final String value;

    RedisKeyEnum(String text, String value) {
        this.text = text;
        this.value = value;
    }

    /**
     * 获取带参数的Redis Key
     * @param param 参数
     * @return 完整的Redis Key
     */
    public String getKey(String param) {
        return this.value + param;
    }

    /**
     * 获取值列表
     * @return
     */
    public static List<String> getValues() {
        return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
    }

    /**
     * 根据 value 获取枚举的 text
     * @param value 状态值
     * @return 状态文本，如果未找到对应的枚举则返回null
     */
    public static String getTextByValue(String value) {
        if (ObjectUtils.isEmpty(value)) {
            return null;
        }
        for (RedisKeyEnum anEnum : RedisKeyEnum.values()) {
            if (anEnum.value.equals(value)) {
                return anEnum.text;
            }
        }
        return null;
    }

    /**
     * 根据 value 获取枚举
     * @param value
     * @return
     */
    public static RedisKeyEnum getEnumByValue(String value) {
        if (ObjectUtils.isEmpty(value)) {
            return null;
        }
        for (RedisKeyEnum anEnum : RedisKeyEnum.values()) {
            if (anEnum.value.equals(value)) {
                return anEnum;
            }
        }
        return null;
    }

    public String getValue() {
        return value;
    }

    public String getText() {
        return text;
    }
}
